package com.ybkj.o2o.zhuanche.controller;

import com.google.common.base.Preconditions;
import com.ybkj.o2o.zhuanche.amqp.DockingSuperviseApi;
import com.ybkj.o2o.zhuanche.common.DataGrid;
import com.ybkj.o2o.zhuanche.common.query.OrderReviewQuery;
import com.ybkj.o2o.zhuanche.common.transport.Error;
import com.ybkj.o2o.zhuanche.common.transport.Result;
import com.ybkj.o2o.zhuanche.entity.Order;
import com.ybkj.o2o.zhuanche.entity.OrderReview;
import com.ybkj.o2o.zhuanche.manager.OrderManager;
import com.ybkj.o2o.zhuanche.manager.OrderReviewManager;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("api/orderReview")
public class OrderReviewRes {

  @Autowired
  private OrderReviewManager orderReviewManager;
  @Autowired
  private OrderManager orderManager;
  
  @Autowired
  private DockingSuperviseApi dockingSuperviseApi;

  /**
   * 创建专车订单评价.
   * @param orderId 订单Id
   * @param content 评价内容
   * @param score 评分
   * @param appKey 区别系统
   */
  @RequestMapping(value = "review", method = RequestMethod.POST)
  public ResponseEntity<Object> create(
      @RequestParam(required = true, value = "orderId") Long orderId,
      @RequestParam(required = false, value = "content") String content,
      @RequestParam(required = true, value = "score") Double score,
      @RequestParam(required = true, value = "appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(score != null, "评价分数不能为空");
    Order order = orderManager.findByIdAndAppKey(orderId, appKey);
    Preconditions.checkArgument(order != null, "订单不存在");
    try {
      OrderReview orderReview = new OrderReview();
      orderReview.setContent(content);
      orderReview.setPassengerId(order.getPassengerId());
      orderReview.setPassengerName(order.getPassengerName());
      orderReview.setEmployId(order.getEmployId());
      orderReview.setEmployName(order.getEmployName());
      orderReview.setScore(score);
      orderReview.setOrderId(order.getId());
      orderReview.setOrderNo(order.getOrderNo());
      orderReview.setCreated(new Date());
      orderReview.setUpdated(new Date());
      orderReview.setAppKey(appKey);
      int insert = orderReviewManager.insert(orderReview);
      if (insert == 1) {
        order.setReview(Boolean.TRUE);
        int update = orderManager.update(order);
        if (update != 1) {
          throw new RuntimeException("修改订单评价信息失败!");
        }
      } else {
        throw new RuntimeException("评价失败!");
      }

      Result<OrderReview> result = new Result<OrderReview>(0, "成功", orderReview);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.CREATED);

      //推送至监管平台
      dockingSuperviseApi.pushCkpjMEssage(order.getOrderNo(),score,content,appKey);
      
      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }

  }

  /**
   * 根据订单id查询订单评价对象.
   */
  @RequestMapping(value = "findReview", method = RequestMethod.GET)
  public ResponseEntity<Object> findReviews(
      @RequestParam("orderId") Long orderId,
      @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(orderId != null && orderId != 0, "订单Id不能为空");
    Order order = orderManager.findByIdAndAppKey(orderId, appKey);
    Preconditions.checkArgument(order != null, "此订单不存在");
    
    OrderReviewQuery orderReviewQuery = new OrderReviewQuery();
    orderReviewQuery.setOrderId(orderId);
    orderReviewQuery.setAppKey(appKey);
    try {

      OrderReview contents = orderReviewManager.findByOrderIdAndAppKey(orderReviewQuery);

      Result<OrderReview> result = new Result<OrderReview>(0, "成功", contents);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }
  
  /**
   * 分页查询单个司机的评价记录.
   * @param driverId 司机id
   */
  @RequestMapping(value = "findByDriver", method = RequestMethod.GET)
  public ResponseEntity<Object> findByDriver(
      @RequestParam("driverId") Long driverId,
      @RequestParam(required = false, value = "page") Integer page,
      @RequestParam(required = false, value = "rows") Integer rows, 
      @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    
    OrderReviewQuery orderReviewQuery = new OrderReviewQuery();
    orderReviewQuery.setDriverId(driverId);
    orderReviewQuery.setPage(page);
    orderReviewQuery.setRows(rows);
    orderReviewQuery.setAppKey(appKey);
    try {

      DataGrid<OrderReview> contents = orderReviewManager.findByDriver(orderReviewQuery);

      Result<DataGrid<OrderReview>> result = new Result<DataGrid<OrderReview>>(0, "成功", contents);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;
    } catch (Exception ex) {

      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }
  
  /**
   * 删除评价信息.
   * @param id 订单评价对象id
   */
  @RequestMapping(value = "deleteById", method = RequestMethod.DELETE)
  public ResponseEntity<Object> delete(
      @RequestParam("id") Long id,
      @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    
    try {

      orderReviewManager.deleteById(id, appKey);
      Result<OrderReview> result = new Result<OrderReview>(0, "删除成功", null);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;
    } catch (Exception ex) {

      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }
  
  /**
   * 查询单个订单评价对象.
   * @param id 订单评价对象id
   */
  @RequestMapping(value = "findOne", method = RequestMethod.GET)
  public ResponseEntity<Object> findOne(
      @RequestParam("id") Long id,
      @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    
    OrderReviewQuery orderReviewQuery = new OrderReviewQuery();
    orderReviewQuery.setId(id);
    orderReviewQuery.setAppKey(appKey);
    
    try {

      OrderReview orderReview = orderReviewManager.findOne(orderReviewQuery);
      Result<OrderReview> result = new Result<OrderReview>(0, "删除成功", orderReview);

      responseEntity = new ResponseEntity<Object>(result, HttpStatus.OK);

      return responseEntity;
    } catch (Exception ex) {

      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }
}
